package com.lagou;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

/**
 * @author kimmy zhang
 * @description JedisClusterTest
 * @date 2021/10/19 10:40 上午
 */
public class JedisClusterTest {
    public static void main(String[] args) {
        // Jedis连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大空闲连接数, 默认8个
        jedisPoolConfig.setMaxIdle(100);
        // 最大连接数, 默认8个
        jedisPoolConfig.setMaxTotal(500);
        //最小空闲连接数, 默认0
        jedisPoolConfig.setMinIdle(0);
        // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
        jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒
        //对拿到的connection进行validateObject校验
        jedisPoolConfig.setTestOnBorrow(true);
        Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7001));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7002));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7003));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7004));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7005));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7006));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7007));
        jedisClusterNode.add(new HostAndPort("192.168.144.131", 7008));
        JedisCluster jcd = new JedisCluster(jedisClusterNode, jedisPoolConfig);
        String key = "name:001";
        System.out.println(jcd.get(key));
        jcd.set(key,"zhangfei");
        
        System.out.println(jcd.get("name:001"));
    }
}
